//
//  SwiperCardViewController.m
//  LottieAnimationTest
//
//  Created by Felix Yin on 2019/4/12.
//  Copyright © 2019 Felix Yin. All rights reserved.
//

#import "SwiperCardViewController.h"
#import "CardView.h"

@interface SwiperCardViewController ()<CardViewDelegate>

@property(nonatomic,assign) NSInteger showMaxCardCount;
@property(nonatomic,strong) NSMutableArray *cardArray;
@property(nonatomic,strong) NSMutableArray *removeArray;
@property(nonatomic,strong) UIButton *rebackBtn;
@property(nonatomic,strong) UIButton *originBtn;
@property(nonatomic,strong) UIButton *animationBtn;
@property(nonatomic,assign) NSInteger currenIndex;
@property(nonatomic,assign) NSInteger maxCount;

@end

@implementation SwiperCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.showMaxCardCount = 3;
    self.maxCount = 1;
    self.removeArray = [[NSMutableArray alloc] init];
    [self layoutViews];
}

- (CardView *) createCardView{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    CGFloat cardX = 100;
    CGFloat cardY = 150;
    CGFloat cardW = width - cardX*2;
    CGFloat cardH = height - 300;
    NSArray *colors = @[[UIColor yellowColor],[UIColor blueColor],[UIColor greenColor],[UIColor blackColor],[UIColor brownColor]];
    CGRect rect = CGRectMake(cardX, cardY, cardW, cardH);//CGRectMake(cardX, cardY+seperateY*i, cardW, cardH);
    CardView *cardView = [[CardView alloc] initWithFrame:rect];
    NSInteger index = (self.removeArray.count+self.cardArray.count);
    cardView.tag = index;
    cardView.delegate = self;
    cardView.num = index;
    cardView.backgroundColor = colors[index];
    return cardView;
}

- (void) layoutViews{
    self.cardArray = [[NSMutableArray alloc] init];
    CGFloat seperateY = 5;
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    CGFloat cardX = 100;
    CGFloat cardY = 150;
    CGFloat cardW = width - cardX*2;
    CGFloat cardH = height - 300;
    NSArray *colors = @[[UIColor yellowColor],[UIColor blueColor],[UIColor greenColor],[UIColor blackColor],[UIColor brownColor]];
    for (int i = 0; i < self.showMaxCardCount; i++) {
        CGRect rect = CGRectMake(cardX, cardY, cardW, cardH);
        CardView *cardView = [[CardView alloc] initWithFrame:rect];
        cardView.tag = i;
        cardView.num = i;
        cardView.delegate = self;
        cardView.backgroundColor = colors[i];
        
        if (i == 0) {
            [self.view addSubview:cardView];
        }else{
            [self.view insertSubview:cardView belowSubview:self.cardArray[i-1]];
        }
        [self.cardArray addObject:cardView];
    }
    
    [self animationCard];
    
    self.rebackBtn = [[UIButton alloc] init];
    self.rebackBtn.backgroundColor = [UIColor redColor];
    self.rebackBtn.frame = CGRectMake((width - 50)*0.5, height - 100, 50, 50);
    [self.rebackBtn setTitle:@"撤回" forState:UIControlStateNormal];
    [self.rebackBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.rebackBtn addTarget:self action:@selector(rebackBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.rebackBtn];
    self.rebackBtn.hidden = YES;
    [self.view addSubview:self.animationBtn];
    [self.view addSubview:self.originBtn];
}

- (void) animationBtnClick:(UIButton *) btn{
    [self animationCard];
}

- (void) originBtnClick:(UIButton *) btn{
    [self animationOrigin];
}

- (void) animationCard{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    CGFloat space = 8;
    CGFloat cardX = 100;
    CGFloat cardY = 150;
    CGFloat cardW = width - cardX*2;
    CGFloat cardH = height - 300;
    for (NSInteger i = 0; i < self.cardArray.count; i++) {
        CardView *view = self.cardArray[i];
        NSLog(@"%@",@(view.tag));
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        CGFloat newCardX = cardX + space * i;
        CGFloat newCardY = cardY + space * i;
        CGFloat newCardW = cardW - space * i*2;
        CGFloat newCardH = cardH;
        view.frame = CGRectMake(newCardX, newCardY, newCardW, newCardH);
        [UIView commitAnimations];
    }
}

- (void) animationOrigin{
//    CGFloat width = [UIScreen mainScreen].bounds.size.width;
//    CGFloat height = [UIScreen mainScreen].bounds.size.height;
//    CGFloat space = 15;
//    CGFloat cardX = 100;
//    CGFloat cardY = 150;
//    CGFloat cardW = width - cardX*2;
//    CGFloat cardH = height - 300;
//    for (NSInteger i = (self.cardArray.count-1); i < self.cardArray.count; i--) {
//        NSLog(@"%@",@(i));
//        CardView *view = self.cardArray[i];
//        [UIView beginAnimations:nil context:nil];
//        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
////        CGFloat scaleX = 1;
////        CGFloat scaleY = 1;
////        CGFloat newCardX = cardX;
////        CGFloat newCardY = cardY - space * i;
////        CGFloat newCardW = cardW - space * i;
////        CGFloat newCardH = cardH;
//        view.frame = CGRectMake(cardX, cardY, cardW, cardH);
//        [UIView commitAnimations];
//    }
}

//    MARK: CardView Delegate

- (void)cardViewRemove:(CardView *)view{
    [self.removeArray addObject:view];
    [self.cardArray removeObject:view];
    self.currenIndex = ([self.cardArray indexOfObject:view]+1);
    self.rebackBtn.hidden = !(self.removeArray.count > 0);
    //检查是否有下一张
    NSInteger count = self.removeArray.count + self.cardArray.count;
    if (count < self.maxCount) {
        CardView *cardView = [self createCardView];
        [self.view insertSubview:cardView belowSubview:self.cardArray.lastObject];
        [self.cardArray addObject:cardView];
    }
    [self animationCard];
}

//    MARK: View Event

- (void) rebackBtnClicked:(UIButton *) btn{
    CardView *cardView = [self.removeArray lastObject];
    [cardView.layer removeAllAnimations];
    [cardView makeUndoAction];
    [self.view addSubview:cardView];
    [self.cardArray insertObject:cardView atIndex:0];
    if (self.cardArray.count > self.showMaxCardCount) {
        CardView *cardView = [self.cardArray lastObject];
        NSLog(@"Remove == %@",@(cardView.num));
        [self.cardArray removeObject:cardView];
        [cardView removeFromSuperview];
    }
    [self.removeArray removeObject:cardView];
    self.rebackBtn.hidden = !(self.removeArray.count > 0);
    [self animationCard];
}

- (UIButton *)animationBtn{
    if (!_animationBtn) {
        _animationBtn = [[UIButton alloc] init];
        CGFloat x = 20;
        CGFloat y = [UIScreen mainScreen].bounds.size.height - 90;
        CGFloat w = 60;
        CGFloat h = 30;
        [_animationBtn setTitle:@"动画" forState:UIControlStateNormal];
        _animationBtn.frame = CGRectMake(x, y, w, h);
        [_animationBtn addTarget:self action:@selector(animationBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _animationBtn.backgroundColor = [UIColor redColor];
    }
    return _animationBtn;
}

- (UIButton *)originBtn{
    if (!_originBtn) {
        CGFloat x = 90;
        CGFloat y = [UIScreen mainScreen].bounds.size.height - 90;
        CGFloat w = 100;
        CGFloat h = 30;
        _originBtn = [[UIButton alloc] init];
        [_originBtn setTitle:@"恢复初始状态" forState:UIControlStateNormal];
        _originBtn.frame = CGRectMake(x, y, w, h);
        [_originBtn addTarget:self action:@selector(originBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _originBtn.backgroundColor = [UIColor grayColor];
    }
    return _originBtn;
}

@end
